package com.dream.service.impl;

import java.text.SimpleDateFormat; 
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dream.able.NoteTakingWallMapper;
import com.dream.datasource.DBContextHolder;
import com.dream.pojo.NoteTakingWall;
import com.dream.pojo.NoteTakingWallExample;
import com.dream.service.able.All_Service_able;

/*
 * 笔记的业务类
 */
@Service("Note_Taking_Wall_Serviceimpl")
public class Note_Taking_Wall_Serviceimpl implements All_Service_able <NoteTakingWall>{
	
	/*
	 * 笔记表的DAO代理对象
	 */
	@Resource(name="noteTakingWallMapper")
	private NoteTakingWallMapper note_taking_wall_mapper;
	
	
	/*
	 * (查询所有有效日记)
	 * @see com.dream.serviceable.Time_axis_Serviceable#listNoteTakingWall()
	 */
	@Override
	public List<NoteTakingWall> list() {
		//切换数据源，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		NoteTakingWallExample note_taking_wall_example=new NoteTakingWallExample();
		//设置排序规则，按年月日降序
		note_taking_wall_example.setOrderByClause("year desc,mon desc,day desc,time desc");
		//创建查询条件工具内部类
		NoteTakingWallExample.Criteria criteria=note_taking_wall_example.createCriteria();
		//设置查询条件
		criteria.andStateEqualTo(1);
		//返回查询到的数据
		return note_taking_wall_mapper.selectByExample(note_taking_wall_example);
	}


	/*
	 * (添加笔记)
	 * @see com.dream.serviceable.Note_Taking_Wall_Serviceable#insertNote(java.lang.String, java.lang.String)
	 */
	@Override
	public NoteTakingWall insert(String ... str){
		//切换数据源，主库
		DBContextHolder.setDbType(DBContextHolder.master);
		//创建时间对象
		Date now = new Date();
		//创建格式化对象
		SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
		//得到现在时间的字符串
		String now_time=ft.format(now);
		//创建笔记对象
		NoteTakingWall note_taking_wall=new NoteTakingWall();
		//插入笔记标题
		note_taking_wall.setTitle(str[0]);
		//插入笔记内容
		note_taking_wall.setContext(str[1]);
		//插入年
		note_taking_wall.setYear(Integer.parseInt(now_time.substring(0, 4)));
		//插入月
		note_taking_wall.setMon(Integer.parseInt(now_time.substring(5, 7)));
		//插入日
		note_taking_wall.setDay(Integer.parseInt(now_time.substring(8,10)));
		//插入状态
		note_taking_wall.setState(1);
		//插入状态
		note_taking_wall.setTime(new Date());
		//将笔记对象，写入数据库
		return note_taking_wall_mapper.insertSelective(note_taking_wall)>0?note_taking_wall:null;
	}


	@Override
	public NoteTakingWall getT(String... str) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
